#include "kernel_operator.h"

constexpr int32_t TOTAL_LENGTH = 8 * 2048;
constexpr int32_t USE_CORE_NUM = 8;
constexpr int32_t BLOCK_LENGTH = TOTAL_LENGTH / USE_CORE_NUM;
constexpr int32_t TILE_NUM = 8;
constexpr int32_t BUFFER_NUM = 2;
constexpr int32_t TILE_LENGTH = BLOCK_LENGTH / TILE_NUM / BUFFER_NUM;

class KernelGELU {
public:
    __aicore__ inline KernelGELU() {}

    __aicore__ inline void Init(GM_ADDR x, GM_ADDR z)
    {
        xGm.SetGlobalBuffer((__gm__ half *)x + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        zGm.SetGlobalBuffer((__gm__ half *)z + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, TILE_LENGTH * sizeof(half));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, TILE_LENGTH * sizeof(half));
        pipe.InitBuffer(tmpBuf0, TILE_LENGTH * sizeof(float));
        pipe.InitBuffer(tmpBuf1, TILE_LENGTH * sizeof(uint8_t));
        pipe.InitBuffer(tmpBuf2, TILE_LENGTH * sizeof(float));
        // pipe.InitBuffer(tmpBuf3, TILE_LENGTH * sizeof(float));
    }

    __aicore__ inline void Process()
    {
        int32_t loopCount = TILE_NUM * BUFFER_NUM;
        for (int32_t i = 0; i < loopCount; ++i) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<half> xLocal = inQueueX.AllocTensor<half>();
        AscendC::DataCopy(xLocal, xGm[progress * TILE_LENGTH], TILE_LENGTH);
        inQueueX.EnQue(xLocal);
    }

    __aicore__ inline void Compute(int32_t progress)
    {
        AscendC::LocalTensor<half> xLocal = inQueueX.DeQue<half>();
        AscendC::LocalTensor<half> zLocal = outQueueZ.AllocTensor<half>();

        AscendC::LocalTensor<float> tmpTensor0 = tmpBuf0.Get<float>();
        AscendC::LocalTensor<uint8_t> sharedTmpBuffer = tmpBuf1.Get<uint8_t>();
        AscendC::LocalTensor<float> xLocal_tmp = tmpBuf2.Get<float>();
        // AscendC::LocalTensor<float> tmpTensor1 = tmpBuf3.Get<float>();


        // 直接计算
        // half a = 0.7978845608028654;
        // half b = 0.044715;
        // half one = 1.0;
        // half scalar = 0.5;
        // AscendC::Mul(tmpTensor0, xLocal, xLocal, TILE_LENGTH); // x^2
        // AscendC::Muls(tmpTensor0, tmpTensor0, b, TILE_LENGTH); // b*x^2
        // AscendC::Adds(tmpTensor0, tmpTensor0, a, TILE_LENGTH); // a + b*x^2
        // AscendC::Mul(tmpTensor0, tmpTensor0, xLocal, TILE_LENGTH); // x*(a + b*x^2)
        // AscendC::Tanh(tmpTensor0, tmpTensor0, sharedTmpBuffer, TILE_LENGTH); // tanh(x*(a + b*x^2))
        // AscendC::Adds(tmpTensor0, tmpTensor0, one, TILE_LENGTH); // tanh(x*(a + b*x^2)) + 1
        // AscendC::Mul(tmpTensor0, tmpTensor0, xLocal, TILE_LENGTH); // x*(tanh(x*(a + b*x^2)) + 1)
        // AscendC::Muls(zLocal, tmpTensor0, scalar, TILE_LENGTH); // 0.5*x*(tanh(x*(a + b*x^2)) + 1)

        // 使用erf函数
        // half b = 0.7071067811865475; // 1/sqrt(2)
        // half one = 1.0;
        // half scalar = 0.5;
        // AscendC::Muls(xLocal, xLocal, b, TILE_LENGTH); // x/sqrt(2)
        // AscendC::Erf<half, false>(xLocal, xLocal, TILE_LENGTH); // erf(x/sqrt(2))
        // AscendC::Adds(xLocal, xLocal, one, TILE_LENGTH); // 1 + erf(x/sqrt(2))
        // AscendC::Muls(zLocal, xLocal, scalar, TILE_LENGTH); // 0.5*(1 + erf(x/sqrt(2))) = gelu(x)

        // 转换为float 使用erf函数
        // float b = 0.7071067811865475f; // 1/sqrt(2)
        // float one = 1.0f;
        // float scalar = 0.5f;
        // AscendC::Cast(tmpTensor0, xLocal, AscendC::RoundMode::CAST_NONE, TILE_LENGTH);
        // AscendC::Muls(tmpTensor0, tmpTensor0, b, TILE_LENGTH); // x/sqrt(2)
        // AscendC::Erf<float, false>(tmpTensor0, tmpTensor0, TILE_LENGTH); // erf(x/sqrt(2))
        // AscendC::Adds(tmpTensor0, tmpTensor0, one, TILE_LENGTH); // 1 + erf(x/sqrt(2))
        // AscendC::Muls(tmpTensor0, tmpTensor0, scalar, TILE_LENGTH); // 0.5*(1 + erf(x/sqrt(2))) = gelu(x)
        // AscendC::Cast(zLocal, tmpTensor0, AscendC::RoundMode::CAST_NONE, TILE_LENGTH);

        // 转换为float 直接计算
        float a = 0.7978845608028654f;
        float b = 0.044715f;
        float one = 1.0f;
        float scalar = 0.5f;
        AscendC::Cast(xLocal_tmp, xLocal, AscendC::RoundMode::CAST_NONE, TILE_LENGTH);
        AscendC::Mul(tmpTensor0, xLocal_tmp, xLocal_tmp, TILE_LENGTH); // x^2
        AscendC::Mul(tmpTensor0, tmpTensor0, xLocal_tmp, TILE_LENGTH); // x^3
        AscendC::Muls(tmpTensor0, tmpTensor0, b, TILE_LENGTH); // b*x^3
        AscendC::Add(tmpTensor0, tmpTensor0, xLocal_tmp, TILE_LENGTH); // x + b*x^3
        AscendC::Muls(tmpTensor0, tmpTensor0, a, TILE_LENGTH); // a*(x + b*x^3)
        AscendC::Tanh(tmpTensor0, tmpTensor0, sharedTmpBuffer, TILE_LENGTH); // tanh(a*(x + b*x^3))
        AscendC::Adds(tmpTensor0, tmpTensor0, one, TILE_LENGTH); // tanh(a*(x + b*x^3)) + 1
        AscendC::Mul(tmpTensor0, tmpTensor0, xLocal_tmp, TILE_LENGTH); // x*(tanh(a*(x + b*x^3)) + 1)
        AscendC::Muls(tmpTensor0, tmpTensor0, scalar, TILE_LENGTH); // 0.5*x*(tanh(a*(x + b*x^3)) + 1)
        AscendC::Cast(zLocal, tmpTensor0, AscendC::RoundMode::CAST_NONE, TILE_LENGTH);

        // 转换为float 使用gelu函数
        // AscendC::Cast(tmpTensor0, xLocal, AscendC::RoundMode::CAST_NONE, TILE_LENGTH);
        // AscendC::Gelu(tmpTensor0, tmpTensor0, TILE_LENGTH);
        // AscendC::Cast(zLocal, tmpTensor0, AscendC::RoundMode::CAST_NONE, TILE_LENGTH);

        // 转换为float 使用erfc函数
        // float b = -0.7071067811865475f; // -1/sqrt(2)
        // float one = 1.0f;
        // float scalar = 0.5f;
        // AscendC::Cast(xLocal_tmp, xLocal, AscendC::RoundMode::CAST_NONE, TILE_LENGTH);
        // AscendC::Muls(tmpTensor0, xLocal_tmp, b, TILE_LENGTH); // -x/sqrt(2)
        // AscendC::Erfc(tmpTensor0, tmpTensor0, sharedTmpBuffer, TILE_LENGTH); // erfc(-x/sqrt(2))
        // AscendC::Mul(tmpTensor0, tmpTensor0, xLocal_tmp, TILE_LENGTH); // x*erfc(-x/sqrt(2))
        // AscendC::Muls(tmpTensor0, tmpTensor0, scalar, TILE_LENGTH); // 0.5*x*erfc(-x/sqrt(2)) = gelu(x)
        // AscendC::Cast(zLocal, tmpTensor0, AscendC::RoundMode::CAST_NONE, TILE_LENGTH);

        // 转换为float 直接计算 优化版本
        // float a = 0.7978845608028654f;
        // float b = 0.044715f;
        // float ab = 0.035677408136300125f; // a*b
        // float one = 1.0f;
        // float scalar = 0.5f;
        // AscendC::Cast(xLocal_tmp, xLocal, AscendC::RoundMode::CAST_NONE, TILE_LENGTH);
        // AscendC::Mul(tmpTensor0, xLocal_tmp, xLocal_tmp, TILE_LENGTH); // x^2
        // AscendC::Mul(tmpTensor0, tmpTensor0, xLocal_tmp, TILE_LENGTH); // x^3
        // AscendC::Muls(tmpTensor0, tmpTensor0, ab, TILE_LENGTH); // ab*x^3
        // AscendC::Muls(tmpTensor1, xLocal_tmp, a, TILE_LENGTH); // a*x
        // AscendC::Add(tmpTensor0, tmpTensor0, tmpTensor1, TILE_LENGTH); // ax + ab*x^3
        // AscendC::Tanh(tmpTensor0, tmpTensor0, sharedTmpBuffer, TILE_LENGTH); // tanh(a*(x + b*x^3))
        // AscendC::Adds(tmpTensor0, tmpTensor0, one, TILE_LENGTH); // tanh(a*(x + b*x^3)) + 1
        // AscendC::Mul(tmpTensor0, tmpTensor0, xLocal_tmp, TILE_LENGTH); // x*(tanh(a*(x + b*x^3)) + 1)
        // AscendC::Muls(tmpTensor0, tmpTensor0, scalar, TILE_LENGTH); // 0.5*x*(tanh(a*(x + b*x^3)) + 1)
        // AscendC::Cast(zLocal, tmpTensor0, AscendC::RoundMode::CAST_NONE, TILE_LENGTH);

        outQueueZ.EnQue<half>(zLocal);
        inQueueX.FreeTensor(xLocal);
    }

    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<half> zLocal = outQueueZ.DeQue<half>();
        AscendC::DataCopy(zGm[progress * TILE_LENGTH], zLocal, TILE_LENGTH);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::GlobalTensor<half> xGm;
    AscendC::GlobalTensor<half> zGm;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf0, tmpBuf1, tmpBuf2;
    // AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf3;
};

extern "C" __global__ __aicore__ void gelu_custom(GM_ADDR x, GM_ADDR z)
{
    KernelGELU op;
    op.Init(x, z);
    op.Process();
}

#ifndef ASCENDC_CPU_DEBUG
void gelu_custom_do(uint32_t blockDim, void *stream, uint8_t *x, uint8_t *z)
{
    gelu_custom<<<blockDim, nullptr, stream>>>(x, z);
}
#endif
